<?php
namespace App\Http\Controllers\Api;

use App\Models\Books;
use App\Models\BooksAutoUpdate;
use App\Http\Controllers\Controller;
use App\Models\BookShelf;
use App\User;

use App\Services\UniPush;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;

class TestController extends Controller{

    public function test()
    {

//        $igt = new UniPush();
//        $title = '您收藏的《元尊》更新啦~';
//        $content = '正文卷 第一千三百四十章 圣龙之气，三分归元';
//        $userList = BookShelf::getUserListByBookId(88);
//        $igt->pushMessageToList($title,$content,UniPush::NOTIFICATION_TEMPLATE,$userList);

        $igt = new UniPush();
        $title = '新版本v1.0.13上线了~';
        $content = '全新版本，增加新功能，修复若干bug!';
//        $data = $igt->pushMessageToAll($title,$content,UniPush::NOTIFICATION_DOWNLOAD_TEMPLATE,'http://sanye666.oss-cn-shenzhen.aliyuncs.com/files/ext_apk/1598637606_5f494626125c4.apk');
        $data = $igt->pushMessageToAll($title,$content,UniPush::NOTIFICATION_TEMPLATE);
        return jsonSuccessData($data);
    }


    public function updateBookListChapterCount()
    {
        //批量更新章节总数
        $bookList = Books::where('new_chapter','!=','')->get();
        foreach ($bookList as $v){
            $chapter_tableName = chapterTableNameByBookId($v['id']);
            /*章节总数*/
            $count = DB::table($chapter_tableName)
                ->whereBook_id($v['id'])
                ->count();
            Books::where('id',$v['id'])->update(['chapter_count'=>$count]);
        }
        return jsonSuccessData($bookList);
    }

    public function test2()
    {

        //获取scrapy 队列数据
        $bookInfo = Redis::lpop(config('cacheKey.scrapyBookUpdate'));
        //出队消费
        if (!empty($bookInfo)){
            $bookInfo = json_decode($bookInfo,true);
            //删除小说详情缓存
            $key = config('cacheKey.bookDetail').$bookInfo['book_id'];
            Redis::del($key);
            //发送邮箱提示
//            $mail_title = '您书架上的《'.$bookInfo['book_name'].'》更新啦！快来追更最新章节吧~';
//            $mail_content = '《'.$bookInfo['book_name'].'》最新章节:<a href="http://www.sanye666.top/chapter?id='.$bookInfo['book_id'].'">'.$bookInfo['new_chapter'].'，点击阅读</a>';
//            Mail::raw($mail_content,function($message)use($mail_title){
//                $to = '244332656@qq.com';
//                $message->to($to)->subject($mail_title);
//            });
//            if(count(Mail::failures()) < 1){
//                return jsonSuccessData($bookInfo);
//            }else{
//                return jsonSuccessData('发送邮件失败，请重试！');
//            }

        }else{
            die('暂无数据');
        }
    }
}
